from tenacity import retry, stop_after_attempt, wait_fixed, RetryError


def error_callback(error_code):
    print("处理三次之后仍然失败，返回None")
    return None


@retry(
    stop=stop_after_attempt(3),  # 最多重试3次
    wait=wait_fixed(2),  # 每次间隔2秒
    reraise=True,
    retry_error_callback=error_callback
)
def fetch_data():
    print("Attempting to fetch data...")
    raise ConnectionError("Network unreachable")


if __name__ == '__main__':
    try:
        fetch_data()
    except Exception as e:
        print(e)
